package com.ns.scs.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ns.core.annotations.ReqLog;
import com.ns.core.bo.IdsBO;
import com.ns.core.utils.StpUtil;
import com.ns.core.utils.jwt.SystemUtil;
import com.ns.core.utils.sg.ResultError;
import com.ns.core.utils.sg.ResultOk;
import com.ns.scs.user.bo.ScsUserBO;
import com.ns.scs.user.bo.ScsUserPageBO;
import com.ns.scs.user.dto.ScsUserPageDto;
import com.ns.scs.user.entry.ScsUser;
import com.ns.scs.user.entry.UserServer;
import com.ns.scs.user.service.IScsUserService;
import com.ns.scs.user.service.IUserServerService;
import com.ns.scs.user.vo.ScsUserVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 业务用户接口
 *
 * @author lw
 */
@RestController
@RequestMapping("/scs/user/")
@RequiredArgsConstructor
@CrossOrigin
public class UsersController {

    private final IScsUserService service;

    private final IUserServerService ursService;

    /**
     * 增
     */
    @PostMapping("add")
    @ReqLog(module = "用户模块", name = "增")
    ResultOk add(@RequestBody ScsUserBO userBO) {
        ScsUser user = new ScsUser();
        BeanUtils.copyProperties(userBO, user);
        user.setSort(service.getMaxSort() + 1);
        if (!service.saveOrUpdate(user)) {
            throw new ResultError("用户保存失败！");
        }

        // 密码加密
        String passwordMd5 = SystemUtil.getPasswordMd5(user.getId(), user.getPwd());
        user.setPwd(passwordMd5);
        if (!service.updateById(user)) {
            throw new ResultError("密码保存失败！");
        }

        // 关联关系
        UserServer us = new UserServer();
        us.setUid(user.getId());
        us.setSid(StpUtil.getSid());
        return ResultOk.getByBoolean(ursService.saveOrUpdate(us));
    }

    /**
     * 删
     *
     * @param id id
     */
    @ReqLog(module = "用户模块", name = "删")
    @GetMapping("delete")
    ResultOk delete(Long id) {
        return ResultOk.getByBoolean(service.removeById(id));
    }

    /**
     * 删 - 根据id列表
     */
    @PostMapping("deleteByIds")
    @ReqLog(module = "用户模块", name = "批量删")
    ResultOk deleteByIds(@RequestBody IdsBO bo) {
        return ResultOk.getByBoolean(service.removeBatchByIds(bo.getIds()));
    }

    /**
     * 改
     *
     * @param userBO 参数
     */
    @PostMapping("update")
    @ReqLog(module = "用户模块", name = "改")
    ResultOk update(@RequestBody ScsUserBO userBO) {
        ScsUser user = new ScsUser();
        BeanUtils.copyProperties(userBO, user);
        // 不允许修改密码
        user.setPwd(null);
        return ResultOk.getByBoolean(service.updateById(user));
    }

    /**
     * 查
     *
     * @param id 参数
     */
    @GetMapping("getById")
    @ReqLog(module = "用户模块", name = "查")
    ResultOk getById(Long id) {
        ScsUser data = service.getById(id);
        ScsUserVO vo = new ScsUserVO();
        BeanUtils.copyProperties(data, vo);
        return ResultOk.getSuccessData(vo);
    }

    /**
     * 查 - 集合
     */
    @PostMapping("getList")
    @ReqLog(module = "用户模块", name = "查 - 集合")
    ResultOk getList(@RequestBody ScsUserPageBO bo) {
        ScsUserPageDto dto = new ScsUserPageDto();
        BeanUtils.copyProperties(bo, dto);
        Page<ScsUserVO> list = service.getList(dto);
        return ResultOk.getPageData(list.getTotal(), list.getRecords());
    }

    /**
     * 查 - options
     */
    @PostMapping("options")
    @ReqLog(module = "用户模块", name = "查 - options")
    ResultOk getOptions(@RequestBody ScsUserPageBO bo) {
        ScsUserPageDto dto = new ScsUserPageDto();
        BeanUtils.copyProperties(bo, dto);
        return ResultOk.getSuccessData(service.getOptions(dto));
    }

    /**
     * 重置密码
     *
     * @param id 参数
     */
    @GetMapping("reset")
    @ReqLog(module = "用户模块", name = "重置密码")
    ResultOk reset(Long id) {
        ScsUser user = service.getById(id);
        user.setPwd(SystemUtil.getPasswordMd5(user.getId(), user.getPhone()));
        return ResultOk.getByBoolean(service.saveOrUpdate(user));
    }
}
